<template>
  <div class="shopcar-container">
    
    <div class="mui-card" v-for="(item, index) in goodsList" :key="item.id">
				<div class="mui-card-content">
					<div class="mui-card-content-inner">
						<mt-switch 
              v-model="$store.getters.getGoodsSwitchChecked[item.id]"
              @change="checkedChange(item.id, $store.getters.getGoodsSwitchChecked[item.id])"
            >
            </mt-switch>
            <img :src="item.img_url" alt="">
            <div class="info">
              <h1>{{ item.title }}</h1>
              <p>
                <span>￥{{ item.price }}</span>
                <ShopCarCom 
                  :initcount="$store.getters.getCarIdCount[item.id]"
                  :goodsId="item.id"
                >
                </ShopCarCom>
                <a href="#" @click.prevent="delGoods(item.id, index)">删除</a>
              </p>
            </div>
					</div>
				</div>
			</div>

      <div class="mui-card">
				<div class="mui-card-content">
					<div class="mui-card-content-inner card-two">
						<div class="left">
              <p>总计(不含运费)</p>
              <p>已勾选商品<span>0</span>件，总价<span>￥0</span></p>              
            </div>
            <mt-button type="danger">去结算</mt-button>
					</div>
				</div>
			</div>

  </div>
</template>

<script>

import ShopCarCom from '../subcomponents/shopcar_numbox.vue'
export default {
  data() {
    return {
      goodsList: []
    }
  },
  created() {
    this.getShopcarList();
  },
  methods:{
    getShopcarList() {  //获取购物车商品
      var idArr = [];
      this.$store.state.shopCar.forEach(item => idArr.push(item.id));  

      if(idArr.length <= 0) {  //若购物车为空，则阻止ajax访问
        return ;
      }
      this.$http.get('./../../../api/shopcar.json?idarr=' + idArr.join(',')).then(res => {
        if(res.body.status === 0) {
          this.goodsList = res.body.message;
        }else {
          alert('error');
        }
      })
    },
    delGoods(id, index) {  //删除商品
      this.$store.commit('remoreGoods',{
        id: id
      })
      this.goodsList.splice(index, 1);
    },
    checkedChange(id, check) {  //监听商品开关状态
      this.$store.commit('reviseGoodsChecked', {
        id: id,
        seleted: check
      })
    }
  },
  components: {
    ShopCarCom
  }
}
</script>

<style>
  .shopcar-container {
    background: #eee;
    overflow: hidden;
  }

  .mui-card-content-inner {
    display: flex;
    align-items: center;
    justify-content: space-between;
  }

  .mui-card-content-inner img{
    width: 60px;
    height: 60px;
  }

  .info p{
    display: flex;
    align-items: center;
    margin-top: 10px;
  }
  .info h1{
    font-size: 13px;
  }

  .info span {
    color: red;
    font-size: 15px;
    font-weight: bold;
  }

  .card-two {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .card-two .left span {
    color: red;
    font-weight: bold;
    font-size: 16px;
  }
</style>
